
package tw.com.whichone.connection;

import java.io.IOException;
import java.io.Reader;
import java.util.Date;
import java.util.Hashtable;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;

public class SqlMapManager {
	protected static Hashtable maps = new Hashtable();

	@SuppressWarnings("unchecked")
	public synchronized static SqlMapClient getInstance(String config)
			throws IOException {
		SqlMapClient map = null;
		String datetimes = null;
		
		map = (SqlMapClient) maps.get(config);
		try{
			datetimes = (String) maps.get("date").toString();
		}catch(Exception e){
			datetimes = null;
		}

		if (datetimes == null) {
			datetimes = "0";
		}

		Long datetimes2 = Long.parseLong(datetimes);

		long nowtime = new Date().getTime();
		if (map == null || datetimes2 == 0
				|| (nowtime - datetimes2) > 11999990) {
			String file = config.replace('.', '/') + ".xml";
			Reader reader = Resources.getResourceAsReader(file);
			map = SqlMapClientBuilder.buildSqlMapClient(reader);
			maps.put(config, map);
			maps.put("date", nowtime);
		}
		maps.put("date", nowtime);
		return map;
	}
}